var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openId: '',
    currentTabsIndex: 0, //我的名片选项卡
    type: ['个人', '企业'],
    cards: "",
    cardTips: '', //页面提示
    holderTips: '',
    cardcaseIndex: 0, //名片夹选项卡
    holders: [],
    personalCardFlag: false,
    companyCardFlag: false,
    holderFlag: false,
  },
  // 显示加载框
  showLoad: function () {
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
  },
  // 隐藏加载框
  hideLoad: function () {
    setTimeout(function () {
      wx.hideLoading();
    }, 500);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      openId: app.globalData.openId,
    })
    if (that.data.currentTabsIndex == 0) {
      var cardUrl = app.globalData.serverAddress + '/api/my/personal/card/' + that.data.openId + '?_t=' + new Date().getTime();
    } else {
      var cardUrl = app.globalData.serverAddress + '/api/my/company/card/' + that.data.openId + '?_t=' + new Date().getTime();
    }
    if (that.data.cardcaseIndex == 0) {
      var holderUrl = app.globalData.serverAddress + '/api/my/card/holder/1/' + that.data.openId + '?_t=' + new Date().getTime();
    } else {
      var holderUrl = app.globalData.serverAddress + '/api/my/card/holder/2/' + that.data.openId + '?_t=' + new Date().getTime();
    }
    console.log('请求获取我的名片' + cardUrl);
    that.showLoad();
    //请求获取我的名片
    app.httpPost(cardUrl, '').then((res) => {
      console.log(res.data);
      if (res.data.flag) {
        that.setData({
          personalCardFlag: res.data.flag,
          cards: res.data.list, //名片集合对象
        })
      } else {
        that.setData({
          cards: '',
          personalCardFlag: res.data.flag,
          cardTips: res.data.msg,
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg);
      wx.showModal({
        title: '提示',
        content: '网络异常',
        showCancel: false,
      })
    });
    console.log('请求获取我的名片夹' + holderUrl);
    //请求获取我的名片夹
    app.httpPost(holderUrl, '').then((res) => {
      console.log(res.data);
      if (res.data.flag) {
        that.setData({
          holders: res.data.list,
          holderFlag: res.data.flag,
        })
      } else {
        that.setData({
          holderFlag: false,
          holderTips: res.data.msg,
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg);
      wx.showModal({
        title: '提示',
        content: '网络异常',
        showCancel: false,
      })
    })
    that.hideLoad();
  },



  /**
   * 点击切换我的名片选项卡
   */
  onTabsItemTap: function (e) {
    var that = this;
    var index = e.target.dataset.index;
    if (that.data.currentTabsIndex === index) {
      return false;
    } else {
      that.setData({
        currentTabsIndex: index,
      })
      //如果切换页面,那么需要分情况获取数据
      if (that.data.currentTabsIndex == 0) {
        var url = app.globalData.serverAddress + '/api/my/personal/card/' + that.data.openId + '?_t=' + new Date().getTime();
      } else {
        var url = app.globalData.serverAddress + '/api/my/company/card/' + that.data.openId + '?_t=' + new Date().getTime();
      }
      app.httpPost(url, '').then((res) => {
        console.log(res.data);
        if (res.data.flag) {
          that.setData({
            personalCardFlag: res.data.flag,
            cards: res.data.list,
          })
        } else {
          that.setData({
            personalCardFlag: res.data.flag,
            cardTips: res.data.msg,
          })
        }
      }).catch((errMsg) => {
        console.log(errMsg);
        wx.showModal({
          title: '提示',
          content: '网络异常',
          showCancel: false,
        })
      })
    }
  },

  /**
   * 点击名片夹选项卡
   */
  cardcase_onTabsItemTap: function (e) {
    var that = this;
    var index = e.target.dataset.index;
    that.setData({
      cardcaseIndex: index
    });
    if (that.data.cardcaseIndex == 0) { //当选项卡在选中个人时,查询名片夹中的个人名片.此时type=1
      var holderUrl = app.globalData.serverAddress + '/api/my/card/holder/1/' + that.data.openId + '?_t=' + new Date().getTime();
    } else { //当选项卡在选中企业时,查询名片夹中的企业名片.此时type=2
      var holderUrl = app.globalData.serverAddress + '/api/my/card/holder/2/' + that.data.openId + '?_t=' + new Date().getTime();
    }
    app.httpPost(holderUrl, '').then((res) => {
      console.log(res.data);
      if (res.data.flag) {
        that.setData({
          holders: res.data.list,
          holderFlag: true,
        })
      } else {
        that.setData({
          holderFlag: false,
          holderTips: res.data.msg,
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg);
      wx.showModal({
        title: '提示',
        content: '网络异常',
        showCancel: false,
      })
    })
  },

  /**
   * 点击我的名片列表
   */
  viewCard: function (options) {
    var that = this;
    console.log(options.currentTarget.dataset);
    var id = options.currentTarget.dataset.id;
    var index = that.data.currentTabsIndex;
    if (id) {
      if (index == 0) {
        wx.navigateTo({ //当前查看个人名片
          url: '/pages/personal/view-card/view-card?cardId=' + id + '&_t=' + new Date().getTime(),
        })
      } else {
        wx.navigateTo({ //当前查看企业名片
          url: '/pages/company/view-card/view-card?cardId=' + id + '&_t=' + new Date().getTime(),
        })
      }
    } else {
      wx.showModal({
        content: '网络异常',
        showCancel: false,
      })
    }
  },

  /**
   * 添加个人卡片
   */
  addPersonalCard: function () {
    var that = this;
    if (that.data.openId) {
      wx.navigateTo({
        url: '/pages/personal/add-card/add-card?openId=' + that.data.openId + '&_t=' + new Date().getTime(),
      })
    } else {
      wx.showModal({
        content: '网络异常',
        showCancel: false,
      })
    }
  },

  /**
   * 添加企业名片
   */
  addCompanyCard: function () {
    var that = this;
    if (that.data.openId) {
      wx.navigateTo({
        url: '/pages/company/add-card/add-card?openId=' + that.data.openId + '&_t=' + new Date().getTime(),
      })
    } else {
      wx.showModal({
        content: '网络异常',
        showCancel: false,
      })
    }
  },

  /**
   * 点击名片夹主体
   */
  viewHolder: function (e) {
    console.log(e.currentTarget.dataset.id);
  },

  /**
   * 跳转到搜索
   */
  jump: function (e) {
    console.log(1);
    var that = this;
    if (that.data.openId) {
      wx.navigateTo({
        url: '/pages/search/search',
      })
    } else {
      wx.showModal({
        content: '网络异常',
        showCancel: false,
      })
    }
  },
  /**
   * 企业卡片点击设置跳转到设置页面
   */
  companySetting: function(e){
    var id = e.currentTarget.dataset.id;
    console.log("id为:"+ id);
    wx.navigateTo({
      url: "/pages/company/edit-card/edit-card?companyId="+id+"&_t="+new Date().getTime(),
    })
  },
  /**
   * 个人卡片点击设置跳转到个人卡片设置
   */
  userSetting: function(e){
    var id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/personal/more-card/more-card?cardId="+id+"&_t="+new Date().getTime(),
    })
  }
})